﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AnaViewer
{
    public partial class ThemeForm : Form
    {
        Properties.Settings settings = Properties.Settings.Default;

        ThemeHelper themeHelper = ThemeHelper.Default;

        public ThemeForm()
        {
            InitializeComponent();

            labelBackColor.Click += new EventHandler(SelectColor_Click);
            labelForeColor.Click += new EventHandler(SelectColor_Click);
            labelHoverBackColor.Click += new EventHandler(SelectColor_Click);

            labelAuthorFont.Click += new EventHandler(SelectFont_Click);
            labelTimeFont.Click += new EventHandler(SelectFont_Click);
            labelTitleFont.Click += new EventHandler(SelectFont_Click);

            chkAuthorVisible.CheckedChanged += new EventHandler(chkAuthorVisible_CheckedChanged);
            radioNothing.CheckedChanged += new EventHandler(Radio_CheckedChanged);
            radioBlankAndWhite.CheckedChanged += new EventHandler(Radio_CheckedChanged);
            radioColorCover.CheckedChanged += new EventHandler(Radio_CheckedChanged);

            btnReset.Enabled = false;
        }
        /// <summary>
        /// 原始样式数据，请勿修改。
        /// </summary>
        ThemeData theme; string themeName = "";
        public void SetTheme(string themeName, ThemeData value)
        {
            this.themeName = themeName; this.theme = value;

            if (themeName == null || themeName.Trim() == "")
                this.Text = "风格编辑器";
            else
                this.Text = string.Format("风格编辑器 - {0}", themeName);

            ImportTheme(theme);
            anaControl1.UpdateTheme(theme);

            this.IsModified = false;
        }

        public ThemeData GetTheme()
        {
            return ExportTheme();
        }
        bool isModified = false;
        public bool IsModified
        {
            get { return isModified; }
            set
            {
                UpdateAnaControl();
                if (isModified == value)
                    return;

                isModified = value;

                btnReset.Enabled = isModified;
            }

        }
        void UpdateAnaControl()
        {
            anaControl1.UpdateTheme(ExportTheme());
        }

        void chkAuthorVisible_CheckedChanged(object sender, EventArgs e)
        {
            groupBoxFaceEffect.Enabled = chkAuthorVisible.Checked;
            this.IsModified = true;
        }

        void Radio_CheckedChanged(object sender, EventArgs e)
        {
            this.IsModified = true;
        }


        void SelectColor_Click(object sender, EventArgs e)
        {
            Label label = sender as Label;

            colorDialog1.Color = label.BackColor;
            DialogResult result = colorDialog1.ShowDialog(this.TopLevelControl);
            if (result == DialogResult.OK)
            {
                label.BackColor = colorDialog1.Color;
            }

            this.IsModified = true;
        }
        private void SelectFont_Click(object sender, EventArgs e)
        {
            Label label = sender as Label;
            fontDialog1.Font = label.Tag as Font;
            DialogResult result = fontDialog1.ShowDialog(this.TopLevelControl);
            if (result == DialogResult.OK)
                SetFont(label, fontDialog1.Font);

            this.IsModified = true;
        }









        void SetFont(Label label, Font font)
        {
            label.Tag = font;
            label.Font = new Font(font.FontFamily, 12, font.Style);
            label.Text = string.Format("{1} 磅, {0}", font.Name, font.Size);
        }
        Font GetFont(Label label)
        {
            Font font = label.Tag as Font;
            return font;
        }


        /// <summary>
        /// 风格，数据 -> 界面
        /// </summary>
        void ImportTheme(ThemeData theme)
        {
            SetFont(labelTitleFont, theme.TitleFont);
            SetFont(labelTimeFont, theme.TimeFont);
            SetFont(labelAuthorFont, theme.AuthorFont);

            labelForeColor.BackColor = theme.ForeColor;
            labelBackColor.BackColor = theme.BackColor;
            labelHoverBackColor.BackColor = theme.HoverColor;

            chkAuthorVisible.Checked = theme.AuthorVisible;

            switch (theme.FaceEffect)
            {
                case HoverMode.Nothing: radioNothing.Checked = true; break;
                case HoverMode.BlankAndWhite: radioBlankAndWhite.Checked = true; break;
                case HoverMode.CoverColor: radioColorCover.Checked = true; break;
                default: radioNothing.Checked = true; break;
            }
        }
        ThemeData ExportTheme()
        {
            ThemeData theme = new ThemeData();

            theme.TitleFont = GetFont(labelTitleFont);
            theme.TimeFont = GetFont(labelTimeFont);
            theme.AuthorFont = GetFont(labelAuthorFont);

            theme.ForeColor = labelForeColor.BackColor;
            theme.BackColor = labelBackColor.BackColor;
            theme.HoverColor = labelHoverBackColor.BackColor;

            theme.AuthorVisible = chkAuthorVisible.Checked;

            if (radioNothing.Checked) theme.FaceEffect = HoverMode.Nothing;
            else if (radioBlankAndWhite.Checked) theme.FaceEffect = HoverMode.BlankAndWhite;
            else if (radioColorCover.Checked) theme.FaceEffect = HoverMode.CoverColor;
            else theme.FaceEffect = HoverMode.Nothing;

            return theme;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if(!IsModified)//没有修改
                return;

            DialogResult result = MessageBox.Show(this.TopLevelControl, "重置操作将使您之前的修改丢失，是否继续？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
                this.SetTheme(themeName,theme);
        }

    }
}